import Tools from '@/utils/Tools.js';
// import Emotion from '@/utils/Emotion.js';
// import Http from '@/utils/Http.js';
// import Api from '@/constant/Api.js';
import store from '@/store/index.js';
import Vue from 'vue'
import moment from '@/utils/Moment.js';
import SmileyList from '@/utils/enum/SmileyList.js';
import { STATIC_FILE_DOMAIN } from '@/constant/Config.js';
class SingleMessageItem {
	avatar = '';
	content = {};
	id = null;
	timestamp = null;
	username = '';
	originalData = {};
	sendStatus = 99;
	uploadStatus = 99;
	uploadProgress = 0;
	uploadTask = null;
	to = null;
	tempFile = ''
	hideTimestamp = false;
	rid = (new Date()).valueOf()
	type = null
	sendSuccess = null
	constructor(arg, isLog = 99) {
		this.originalData = arg
		this.avatar = arg.avatar;
		if (typeof arg.content == 'string') {
			this.content = JSON.parse(Tools.escape2Html(arg.content));
		} else {
			this.content = arg.content;
		}
		this.id = arg.id;
		this.type = arg.type || 'single';
		this.timestamp = arg.timestamp;
		this.username = arg.username;
		this.hideTimestamp = arg.hideTimestamp || false
		this.sendSuccess = arg.sendSuccess || null;
		this.is_recall = arg.is_recall || 0;
		if (!isLog) {
			this.sendStatus = 0;
			this.tempFile = this.content.text;
		}
	}

	sendMessage(to, type = 'single') {
		if (this.sendStatus == 99) {
			return false;
		}
		this.type = type;
		this.to = to;
		switch (this.content.type) {
			case 'image':
				this.startUploadImage()
				break;
			case 'video':
				this.startUploadVideo()
				break;
			case 'emotion':
				this.send();
				break;
			case 'location':
				this.send();
				break;
			case 'voice':
				this.startUploadVoice()
				break;
			case 'text':

				this.handleTextEmotion()
				break;
			default:
				this.send();
		}

	}

	reSend() {
		this.timestamp = parseInt(moment().valueOf() / 1000);
		this.sendStatus = 0;
		setTimeout(() => {
			this.send()
		}, 400)

	}
	send() {
		if (!this.to) {
			this.sendStatus = -1;
		}
		this.uploadStatus = 99;
		const msgData = {
			type: 'chatMessage',
			data: {
				mine: {
					id: store.state.user.info.user_id,
					content: JSON.stringify(this.content)
				},
				to: this.to
			}

		}
		let requestPromise = null;
		if (this.type == 'single') {
			requestPromise = Vue.prototype.$u.api.sendChatSingleMessage;
		} else {
			requestPromise = Vue.prototype.$u.api.sendChatGroupMessage;
		}
		requestPromise(this.content.randstr, JSON.stringify(this.content), this.to).then(res => {
			if (res.code == 200) {
				this.sendStatus = 99;
			} else {
				this.sendStatus = -1;
			}
			this.sendSuccess && this.sendSuccess()
		}).catch(e => {
			this.sendStatus = -1;
			this.sendSuccess && this.sendSuccess()
		});
	}
	handleTextEmotion() {
		const reg = /\[([^\]]+)\]/g;
		const text = this.content.text;
		const str = text.replace(reg, match => {
			for (let i = 0; i < SmileyList.length; i++) {
				if (SmileyList[i].cn == match) {
					return `|${STATIC_FILE_DOMAIN}/static/smiley/${SmileyList[i].fileName}|`;
					break;
				}
			}
		});
		const xml = str.split('|').filter(function(s) {
			return s && s.trim();
		}).join(',');
		if (text != xml) {
			Vue.set(this.content, 'xml', xml);
		}
		this.send();
	}
	startUploadVoice() {
		this.uploadStatus = 0;
		Vue.prototype.$u.api.uploadAudio(this.content.text).then(res => {
			if (res.code == 200) {
				try {
					Vue.set(this.content, 'text', res.data.file_path);
					Vue.set(this.content, 'duration', Number(res.data.duration).toFixed(2));
					setTimeout(() => {
						this.send();
					}, 100)
				} catch (e) {
					this.uploadStatus = -1;
				}

			} else {
				this.uploadStatus = -1;
				this.uploadTask = null
			}
		});
	}
	startUploadImage() {
		this.uploadStatus = 0;

		Vue.prototype.$u.api.uploadImage(this.content.text).then(res => {
			if (res.code == 200) {
				Vue.set(this.content, 'text', res.data.file_path);
				this.send();
			} else {
				this.uploadStatus = -1;
				this.uploadTask = null
			}
		});
		// this.uploadTask.onProgressUpdate(res => {
		// 	this.uploadProgress = res.progress
		// })
	}
	startUploadVideo() {
		this.uploadStatus = 0;
		Vue.prototype.$u.api.uploadVideo(this.content.text).then(res => {
			if (res.code == 200) {
				Vue.set(this.content, 'text', res.data.file_path);
				Vue.set(this.content, 'cover', res.data.thumb);
				setTimeout(() => {
					this.send();
				}, 100)
			} else {
				this.uploadStatus = -1;
				this.uploadTask = null
			}
		});
	}
	stopUploadTask() {
		if (this.uploadTask) {
			this.uploadTask.abort();
		}
	}


}

export {
	SingleMessageItem
}
